//
//  QuadNode.m
//  Assignment1Q3Q4
//
//  Created by LxL on 2012-10-17.
//
//

#import "QuadNode.h"

@implementation QuadNode

@synthesize bounding=_bounding;
@synthesize childrens=_childrens;
@synthesize parentNode=_parentNode;


//This method will create node in QuadTree
-(void) CreateNodefrom:(QuadNode *)parentNode
            withBoundingBox:(CGRect)boundingBox
{
    self.parentNode=parentNode;
    self.bounding=boundingBox;
    
    int tmpMinBounding= MIN(boundingBox.size.height, boundingBox.size.width);
    
    if (tmpMinBounding/2>MIN_SIZE)
    {
        QuadNode *node1=[[QuadNode alloc]init];
        [node1 CreateNodefrom:self withBoundingBox:CGRectMake(boundingBox.origin.x,boundingBox.origin.y+boundingBox.size.height/2, boundingBox.size.width/2, boundingBox.size.height/2)];
        QuadNode *node2=[[QuadNode alloc]init];
        [node2 CreateNodefrom:self withBoundingBox:CGRectMake(boundingBox.origin.x+boundingBox.size.width/2,boundingBox.origin.y+boundingBox.size.height/2, boundingBox.size.width/2, boundingBox.size.height/2)];
        QuadNode *node3=[[QuadNode alloc]init];
        [node3 CreateNodefrom:self withBoundingBox:CGRectMake(boundingBox.origin.x, boundingBox.origin.y, boundingBox.size.width/2, boundingBox.size.height/2)];
        QuadNode *node4=[[QuadNode alloc]init];
        [node4 CreateNodefrom:self withBoundingBox:CGRectMake(boundingBox.origin.x+boundingBox.size.width/2, boundingBox.origin.y, boundingBox.size.width/2, boundingBox.size.height/2)];
        self.childrens= [NSArray arrayWithObjects:node1,node2,node3,node4,nil];
    }else{
        self.childrens=nil;
    }
    self.contentList= [[NSMutableArray alloc]init];
}

//This method add object to QuadNode
-(void) AddContent:(NSObject*) content
{
    bool canAddToChild=NO;
    if(self.childrens!=nil)
    {
        for(QuadNode *node in self.childrens)
        {
            if(CGRectContainsRect(node.bounding,((SquareSprite*)content).boundingBox))
            {
                canAddToChild=YES;
                [node AddContent:content];
            }
        }
    }
    if(canAddToChild==NO)
    {
        [self.contentList addObject:content];
        if(((SquareSprite*)content).contentNode!=nil)
        {
            ((SquareSprite*)content).contentNode =nil;
        }
        ((SquareSprite*)content).contentNode=self;
        
    }
    
}

//This Method remove object from QuadNode. It is used when update QuadTree
-(void) DeleteContent:(NSObject *)content
{   
    [self.contentList removeObject:content];
}

//This method is used to update object between nodes.
-(void) UpdateContent:(NSObject*) content
{
    if(CGRectContainsRect(self.parentNode.bounding,((SquareSprite*)content).boundingBox))
    {
        [self.parentNode AddContent:content];
    }else
    {
        [self.parentNode UpdateContent:content];
    }
}

//This method is used to get the list of objects in a QuadNode and its children
-(void) GetContentByTraverseDownAndPutResultIn:(NSMutableArray**)result
{
    [*result addObjectsFromArray:self.contentList];
    if(self.childrens!=nil)
    {   
        for(QuadNode *node in self.childrens)
        {
            [node GetContentByTraverseDownAndPutResultIn:result];
        }
    }
}

@end
